using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using ZoneFiveSoftware.Common.Visuals;
using FilteredStatistics.Controller;
using FilteredStatistics.Data;
using System.Windows.Forms;
using System.Diagnostics;

namespace FilteredStatistics.View
{
    class FilteredStatisticsRowDataRenderer : ExtendedRowDataRenderer
    {
        public FilteredStatisticsRowDataRenderer(TreeList treeList) : base(treeList)
        {
        }

        protected override TreeList.DefaultRowDataRenderer.RowDecoration GetRowDecoration(object element)
        {
            IList rows = GetRows();

            if(rows.IndexOf(element) == rows.Count - 1)
            {
                // Totals rows, highlight up
                return RowDecoration.TopLineSingle;
            }
            else if(rows.IndexOf(element) == rows.Count - 2)
            {
                // Last range row, highlight down for thick line
                return RowDecoration.BottomLineSingle;
            }

            return RowDecoration.None;
        }

        protected override FontStyle GetCellFontStyle(object element, TreeList.Column column)
        {
            IList rows = GetRows();

            if (rows.IndexOf(element) == rows.Count - 1)
            {
                // Totals rows, draw in bold
                return FontStyle.Bold;
            }

            return base.GetCellFontStyle(element, column);
        }

        protected override void DrawCell(Graphics graphics, TreeList.DrawItemState rowState, object element, TreeList.Column column, Rectangle cellRect)
        {
            if (Options.Instance.DrawColumnsColored)
            {
                TreeListColumnInfo columnInfo = Options.Instance.StatisticsColumnsManager.FindColumn(new Guid(column.Id));
                Brush columnTextBrush;
                Font drawFont = Font(GetCellFontStyle(element, column));
                String displayString = string.Empty;

                if (rowState.Selected)
                {
                    columnTextBrush = BrushCellTextSelected;
                }
                else
                {
                    columnTextBrush = new SolidBrush(columnInfo.Provider.GetColumnColor(columnInfo.Id));
                }

                displayString = LabelProvider.GetText(element, column);
                graphics.DrawString(displayString, drawFont, columnTextBrush, cellRect, GetCellStringFormat(element, column));

                if (!rowState.Selected)
                {
                    columnTextBrush.Dispose();
                }
            }
            else
            {
                base.DrawCell(graphics, rowState, element, column, cellRect);
            }
        }
    }
}
